package com.ctsi.framework.security.util;

import com.ctsi.commons.util.UtilMisc;
import com.ctsi.framework.organise.security.SecurityUser;
import com.ctsi.framework.organise.security.UserInfo;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.util.LinkedHashMap;

/**
 * @Auther: zhangjw
 * @Date: 2018/7/25 17:11
 * @Description: 获取登陆用户
 */
public abstract class SecurityUtil {

    /**
     * 获取当前登陆用户
     *
     * @return
     */
    public static UserInfo getUser() {

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (auth instanceof OAuth2Authentication) {
            OAuth2Authentication oauth = (OAuth2Authentication)auth;
            UsernamePasswordAuthenticationToken userToken = (UsernamePasswordAuthenticationToken)oauth.getUserAuthentication();
            Object principal = userToken.getPrincipal();
            if (principal instanceof SecurityUser){
                SecurityUser user = (SecurityUser) principal;
                return user.getUserInfo();
            }else if (principal instanceof String){
                LinkedHashMap<String,Object> detail = (LinkedHashMap<String, Object>) userToken.getDetails();
                LinkedHashMap<String,Object> pm = (LinkedHashMap<String, Object>) detail.get("principal");
                LinkedHashMap<String,Object> um = (LinkedHashMap<String, Object>) pm.get("userInfo");
                UserInfo userInfo = new UserInfo();
                UtilMisc.copyMapToBean(um, userInfo,true);
                return userInfo;
            }
        }

        return null;
    }
}

//        Object obj = auth.getPrincipal();
//
//        if(obj instanceof SecurityUser){
//            return (SecurityUser)obj;
//        }else if(obj instanceof String){
//            String principal = (String)obj;
//            if ("anonymousUser".equalsIgnoreCase(principal)){
//                return null;
//            } else {
//                Object o = auth.getDetails();
//                if (o instanceof Map){
//                    Map<String,Object> usrMap = (Map<String, Object>) o;
//                    Map<String,Object> user = (Map<String, Object>) usrMap.get("principal");
//                    return new SecurityUser((String) user.get("username"),
//                            null,
//                            (Boolean)user.get("enabled"),
//                            (Boolean)user.get("accountNonExpired"),
//                            (Boolean)user.get("credentialsNonExpired"),
//                            (Boolean) user.get("accountNonLocked"),
//                            (Collection<? extends GrantedAuthority>)user.get("authorities"),
//                            (Long) user.get("userId"),
//                            (Long) user.get("organiseId"),
//                            (String)user.get("organName"),
//                            (String)user.get("firstName"),
//                            (String)user.get("lastName"),
//                            (String)user.get("fullName"),
//                            (String)user.get("gender"),
//                            (String)user.get("status"),
//                            (String)user.get("nickName"));
//                }else{
//                    return null;
//                }
//            }
//        }
//
//        return null;
